package com.honglang.module.album.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.honglang.common.tools.Constants;
import com.honglang.common.tools.Pager;
import com.honglang.module.album.dao.PhotoDao;
import com.honglang.module.album.entity.Photo;

public class PhotoServiceImpl implements PhotoService{
	
	private PhotoDao photoDao;
	
	/**
	 * save photo entities to db
	 * @param realContextPath  the context path of the project;
	 * @param fileNameList
	 */
	public void addPhoto(String realContextPath,List<String> fileNameList){
		
		List<Photo> photoList = new ArrayList<Photo>();
		
		if(null != fileNameList){
			for(String fileName : fileNameList){
				
				
				if(Constants.ImageTypesList.contains(fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase())){
					String relativeUrl ="";
					relativeUrl = Constants.UploadFileRootDir + "/" +Constants.ImageFileDir + "/" + fileName;
					Photo photo = new Photo();
					photo.setUrl(relativeUrl);
					photo.setAbsoluteUrl(realContextPath + relativeUrl  );
					photo.setFileName(fileName);
					photo.setSuffix(fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase());
					photo.setUploadDate(new Date());
					photoList.add(photo);
				}
			}
			this.getPhotoDao().batchSaveOrUpdate(photoList);
		}
		
	}
	
	
	/**
	 *  get one page of photos
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public Pager getPager(int pageSize,int pageNo){
		int rowCount = this.photoDao.getRowCountOfAllEntites();
		Pager pager = new Pager();
		pager.CreatePager(pageSize, pageNo, Constants.pageIndexCountPerPage, rowCount, this.photoDao.getEntitiesPerPage(pageSize, pageNo));
		return pager;
	}
		
	
	
	/**
	 * get Photo Entity by id
	 * @param id
	 * @return
	 */
	public Photo getPhoto(Integer id){
		return this.getPhotoDao().getById(id);
	}
	
	
	/**
	 * delete photo
	 * @param photo
	 */
	public void delete(Photo photo){
		this.getPhotoDao().delete(photo);
	}
	
	
	public PhotoDao getPhotoDao() {
		return photoDao;
	}

	public void setPhotoDao(PhotoDao photoDao) {
		this.photoDao = photoDao;
	}

	
	
}
